package com.zmn.plat.business.impl.service.item.servicetime;


import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.business.interfaces.service.item.servicetime.ServServiceTimeBService;
import com.zmn.plat.model.entity.service.item.servicetime.ServServiceTime;
import com.zmn.plat.model.entity.service.item.servicetime.ServServiceTimeQuery;
import com.zmn.plat.services.interfaces.service.item.servicetime.ServServiceTimeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 描述: 项目服务时长业务接口
 *
 * @author yangshuangping
 * @since 2021/07/27 10:36
 */
@Service
@Slf4j
public class ServServiceTimeBServiceImpl implements ServServiceTimeBService {

    @Resource
    ServServiceTimeService serviceTimeService;

    @Override
    public Integer getServiceMinutesByItemIdAndCompanyId(List<Integer> itemIds, Integer companyId) {
        if (CollectionUtil.isNullOrEmpty(itemIds)) {
            return GlobalConsts.NONE;
        }
        ServServiceTimeQuery query = new ServServiceTimeQuery();
        query.setItemIds(itemIds);
        query.setCompanyId(companyId);
        List<ServServiceTime> list = serviceTimeService.listByQuery(query);
        if (CollectionUtil.isNotNullOrEmpty(list)) {
            return list.stream()
                    .filter(e -> Objects.nonNull(e.getServiceMinutes()))
                    .map(ServServiceTime::getServiceMinutes)
                    .collect(Collectors.summarizingInt(Integer::intValue)).getMax();
        }

        return GlobalConsts.NONE;
    }

    @Override
    public Map<Integer, Integer> setServiceDuration(List<Integer> itemIds, Integer companyId) {

        if (CollectionUtil.isNullOrEmpty(itemIds) || NumberUtil.isNullOrZero(companyId)) {
            return null;
        }
        ServServiceTimeQuery query = new ServServiceTimeQuery();
        query.setItemIds(itemIds);
        query.setCompanyId(companyId);
        List<ServServiceTime> list = serviceTimeService.listByQuery(query);
        if (CollectionUtil.isNotNullOrEmpty(list)) {
            return list.stream().collect(Collectors.toMap(ServServiceTime::getItemId, ServServiceTime::getServiceMinutes));
        }
        return null;
    }
}

